package com.tsd.storage;

import com.tsd.common.dao.CommonStorageMapper;
import com.tsd.common.entity.CommonStorageExt;
import com.tsd.core.storage.Storage;
import com.tsd.core.utils.CharUtil;
import com.tsd.core.utils.FileUtil;
import org.springframework.core.io.Resource;

import java.io.InputStream;
import java.nio.file.Path;
import java.util.Date;
import java.util.stream.Stream;


/**
 * 提供存储服务类，所有存储服务均由该类对外提供
 *
 * @author Hillpool
 */
public class StorageService {
    /**
     * 存储类型：local
     */
    final public static String STORE_TYPE_LOCAL = "local";
    final public static String STORE_TYPE_ALIYUN = "aliyun";
    final public static String STORE_TYPE_TENCENT = "tencent";
    final public static String STORE_TYPE_QINIU = "qiniu";
    @javax.annotation.Resource
    private CommonStorageMapper commonStorageMapper;

    private String active;
    private Storage storage;

    public String getActive() {
        return active;
    }

    public void setActive(String active) {
        this.active = active;
    }

    public Storage getStorage() {
        return storage;
    }

    public void setStorage(Storage storage) {
        this.storage = storage;
    }

    /**
     * 存储一个文件对象
     *
     * @param inputStream   文件输入流
     * @param contentLength 文件长度
     * @param contentType   文件类型
     * @param root          根目录
     * @param fileName      文件索引名
     */
    public String store(InputStream inputStream, long contentLength, String contentType, String root, String fileName) {
        String key = generateKey(root, fileName);
        storage.store(inputStream, contentLength, contentType, key);

        String url = generateUrl(key);
        CommonStorageExt storageInfo = new CommonStorageExt();
        storageInfo.setName(FileUtil.getFileNameWithoutPath(fileName));
        storageInfo.setSize((int) contentLength);
        storageInfo.setType(contentType);
        storageInfo.setKey(key);
        storageInfo.setUrl(url);
        storageInfo.setAdd_time(new Date());
        storageInfo.setUpdate_time(new Date());
        commonStorageMapper.insertSelective(storageInfo);
        return url;
    }

    private String generateKey(String accountSetSid, String originalFilename) {
        int index = originalFilename.lastIndexOf('.');
        String suffix = index > -1 ? originalFilename.substring(index) : "";
        int pathLastIndex = originalFilename.lastIndexOf("/");
        String path = pathLastIndex > -1 ? originalFilename.substring(0, pathLastIndex) : null;
        String key = null;
        CommonStorageExt storageInfo = null;
        do {
            String id = CharUtil.getRandomString(20);
            key = (accountSetSid == null ? "" : accountSetSid + "/") + (path == null ? id : path + "/" + id) + suffix;
            storageInfo = commonStorageMapper.selectByKey(key);
        } while (storageInfo != null);
        return key;
    }

    public Stream<Path> loadAll() {
        return storage.loadAll();
    }

    public Path load(String keyName) {
        return storage.load(keyName);
    }

    public Resource loadAsResource(String keyName) {
        return storage.loadAsResource(keyName);
    }

    public void delete(String keyName) {
        storage.delete(keyName);
    }

    private String generateUrl(String keyName) {
        return storage.generateUrl(keyName);
    }
}
